package com.example.wannerone.myapplication123;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatRadioButton;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.content.DialogInterface;
import android.widget.CheckBox;
import android.widget.RadioButton;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.wannerone.http.HttpPostUtils;

import java.util.HashMap;
import java.util.Map;

public class linshikeyActivity extends Activity {

    private String result;

    Button btn;
    //一次性密码
    CheckBox checkBox2;
    //时限密码
    CheckBox checkBox;
    //临时密码期限
    RadioButton radioButton3;//1天
    RadioButton radioButton;//3天
    RadioButton radioButton2;//7天
    RadioButton radioButton4;//30天

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linshikey);

        btn = (Button) findViewById(R.id.button7);
        radioButton = (RadioButton) findViewById(R.id.radioButton);
        radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
        radioButton4 = (RadioButton) findViewById(R.id.radioButton4);

        checkBox = (CheckBox) findViewById(R.id.checkBox);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        //全局变量
        final MyApplication application = (MyApplication) getApplication();
        System.out.println("application.getU_id=" + application.getU_id());
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String u_id=null;
                String available_times=null;
                String failure_time=null;
                if (checkBox2.isChecked()) {
                    //一次性密码
                    u_id = String.valueOf(application.getU_id());
                    available_times = "1";
                    failure_time = "";
                } else {
                    if (radioButton3.isChecked()) {
                        u_id = String.valueOf(application.getU_id());
                        available_times = "-2";
                        failure_time = "1";
                    } else if (radioButton.isChecked()) {
                        u_id = String.valueOf(application.getU_id());
                        available_times = "-2";
                        failure_time = "3";
                    } else if (radioButton2.isChecked()) {
                        u_id = String.valueOf(application.getU_id());
                        available_times = "-2";
                        failure_time = "7";
                    } else if (radioButton4.isChecked()) {
                        u_id = String.valueOf(application.getU_id());
                        available_times = "-2";
                        failure_time = "30";
                    }
                }
                if(u_id!=null && u_id!="" && available_times!=null && failure_time!=null){
                    final Map<String, Object> userInfo = new HashMap<String, Object>();
                    userInfo.put("u_id", u_id);
                    userInfo.put("available_times", available_times);
                    userInfo.put("failure_time",failure_time);
                    String jsonString = JSONObject.toJSONString(userInfo);
                    final String info = "info=" + jsonString;
                    /*这里需要留意的是httpPostUtils请求在Android里面不能放在主线程里面，必须新建一个子线程，然后通过Hanlder把子线程的值传过来更新UI（因为子线程不能直接更改UI）*/
                    new Thread() {
                        public void run() {
                            String response = HttpPostUtils.doPostRequest("/doorKey/addTempraryKey", info);
                            Message message = Message.obtain();
                            message.obj = response;
                            mHanlder.sendMessage(message);
                        }
                    }.start();
                }
            }
        });
    }


    private Handler mHanlder = new Handler() {
        @Override
        public void handleMessage(Message message) {
            result = (String) message.obj;
            JSONObject resultJson = JSON.parseObject(result);
            String finalResult = resultJson.getString("result");
            System.out.println("结果是：" + finalResult);
            if (finalResult.equals("ok")) {
                String password1 = resultJson.getString("password");
                System.out.println("临时密码添加成功");

                new AlertDialog.Builder(linshikeyActivity.this)
                        .setTitle("生成成功")
                        .setMessage("临时密码为:"+password1)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(linshikeyActivity.this, functionActivity.class);
                                //启动意图
                                startActivity(intent);
                                linshikeyActivity.this.finish();
                            }   //确定按钮的点击事件
                        })
                        .show();
                //建立一个意图,参数为（当前的Activity类对象，需要开的的Activity类）
//                Intent intent = new Intent(Main2Activity.this, functionActivity.class);
//                //启动意图
//                startActivity(intent);
//                Main2Activity.this.finish();
            } else
                System.out.println("临时密码生成错误");
        }
    };
}
